package com.ct.rbac.repository.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ct.rbac.convert.SysRoleConvertDOToModel;
import com.ct.rbac.mapper.SysRolesMapper;
import com.ct.rbac.model.SysRoleModel;
import com.ct.rbac.orm.SysRolesDO;
import com.ct.rbac.repository.ISysRolesRespontory;
import com.ct.repository.commons.BaseServiceImpl;
import com.ct.repository.commons.PageUtils;
import com.ct.utils.threadlocal.UserThreadLocal;
import jakarta.annotation.Resource;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 角色表 服务实现类
 * </p>
 *
 * @author Auto Generator
 * @since 2023-08-13
 */
@Service
public class SysRolesRespontoryImpl extends BaseServiceImpl<SysRolesMapper, SysRolesDO> implements ISysRolesRespontory {

    @Resource
    private SysRoleConvertDOToModel sysRoleConvertDOToModel;

    @Resource
    private SysRolesMapper sysRolesMapper;

    /**
     * 分页查询角色-系统角色
     * @param pageNo
     * @param pageSize
     * @param status
     * @param keyword
     * @return
     */
    @Override
    public PageUtils<SysRoleModel> findPage(Integer pageNo, Integer pageSize, Integer status, String keyword){
        Page<SysRolesDO> page = new Page<>(pageNo, pageSize);
        LambdaQueryWrapper<SysRolesDO> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(Objects.nonNull(status), SysRolesDO::getStatus, status);
        lambdaQueryWrapper.like(StringUtils.isNoneEmpty(keyword), SysRolesDO::getRoleName, keyword);
        Page<SysRolesDO> doPage = this.page(page, lambdaQueryWrapper);
        return tranferPageUtilsBo(doPage, SysRoleModel.class);
    }

    /**
     * 分页查询角色--租户角色
     * @param pageNo
     * @param pageSize
     * @param status
     * @param keyword
     * @return
     */
    @Override
    public PageUtils<SysRoleModel> findPageSystem(Integer pageNo, Integer pageSize, Integer status, String keyword){
        Page<SysRolesDO> page = new Page<>(pageNo, pageSize);
        LambdaQueryWrapper<SysRolesDO> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(SysRolesDO::getSystemId, UserThreadLocal.get().getSystemId());
        lambdaQueryWrapper.eq(Objects.nonNull(status), SysRolesDO::getStatus, status);
        lambdaQueryWrapper.like(StringUtils.isNoneEmpty(keyword), SysRolesDO::getRoleName, keyword);
        Page<SysRolesDO> doPage = this.page(page, lambdaQueryWrapper);
        return tranferPageUtilsBo(doPage, SysRoleModel.class);
    }

    /**
     * 根据用户查询角色信息
     * @param userId
     * @return
     */
    @Override
    public List<SysRoleModel> findRolesByUserId(Long userId){
        List<SysRolesDO> roles = sysRolesMapper.findRoles(userId);
        return sysRoleConvertDOToModel.convert(roles);
    }

}
